﻿using JNPF.Basics.Models;
using JNPF.Data;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 采购原材料
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-07-10 
    /// </summary>
    internal class ProcurementMaterialService : DataFactory, ProcurementMaterialIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<List<ProcurementEntryEntity>> GetProcurementEntryList(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<ProcurementEntryEntity>().Where(m => m.ProcurementId == id).OrderBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<ProcurementMaterialEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<ProcurementMaterialEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="procurementEntryEntityList">子表</param>
        public async Task Create(ProcurementMaterialEntity entity, List<ProcurementEntryEntity> procurementEntryEntityList)
        {
            using (var db = this.Basic)
            {
                //await  db.BeginTransactionAsync();
                await db.InsertAsync(entity);
                await db.InsertAsync(procurementEntryEntityList);
                //await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="procurementEntryEntityList">子表</param>
        public async Task Update(ProcurementMaterialEntity entity, List<ProcurementEntryEntity> procurementEntryEntityList)
        {
            using (var db = this.Basic)
            {
                //await db.BeginTransactionAsync();
                await db.UpdateAsync(entity);
                await db.DeleteAsync<ProcurementEntryEntity>(m => m.ProcurementId == entity.Id);
                await db.InsertAsync(procurementEntryEntityList);
                //await db.CommitTransactionAsync();
            }
        }
    }
}
